package com.fh.controller;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/12/518:03
 */

import com.fh.common.yyqData;
import com.fh.entity.po.Org;
import com.fh.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * created by macro on 2021/12/5,
 */
@RestController
@RequestMapping("org")
public class OrgController {
    @Autowired
    private OrgService orgService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("upOrg")
    public yyqData upOrg(Org org){
        if (org.getId()==null){
            return yyqData.error(400, "缺少参数");
        }
        orgService.upOrg(org);
        this.reflushOrg();
        return yyqData.success("");
    }

    @PostMapping("addOrg")
    public yyqData addOrg(Org org){
        if (StringUtils.isEmpty(org.getName())){
            return yyqData.error(400, "缺少参数");
        }
        orgService.addOrg(org);
        this.reflushOrg();
        return yyqData.success("");
    }

    @GetMapping("queryOrg")
    public yyqData queryOrg(){
        return yyqData.success(orgService.queryOrg());
    }

    @GetMapping("queryOrgAll")
    public yyqData queryOrgAll(){
        return yyqData.success(orgService.queryOrgAll());
    }

    @GetMapping("reflushOrg")
    public yyqData reflushOrg(){
        //清除缓存
        redisTemplate.delete("orgData");
        // 添加缓存
        List<Org> orgs = orgService.queryOrg();
        redisTemplate.opsForValue().set("orgData",orgs);
        return yyqData.success("");
    }

}
